package com.ktjy.config;

import com.ktjy.entity.SysRight;
import com.ktjy.entity.SysUser;
import com.ktjy.entity.vo.SysUserVo;
import com.ktjy.service.ISysRoleRightService;
import com.ktjy.service.ISysUserService;
import com.ktjy.utils.MySimpleByteSource;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lqm
 * @ClassName CustoRealm
 * @description: shiro  链接数据库的工具类
 */
public class CustoRealm extends AuthorizingRealm {

    @Resource
    ISysUserService sysUserService;

    @Autowired
//    RedisTemplate redisTemplate;


    @Resource
    ISysRoleRightService sysRoleRightService;

    /**
     * 授权域  查询认证的主体可访问的权限
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info =new  SimpleAuthorizationInfo();

        SysUserVo sysUser= (SysUserVo) principalCollection.getPrimaryPrincipal();

//        info.addRole(String.valueOf(sysUser.getUsrRoleId()));
//        List<String> resources= sysRoleRightService.getRightByRoleId(sysUser.getUsrRoleId());

//        info.addStringPermissions(resources);

        if(sysUser.getRole().getRights()!=null){
            for ( SysRight right: sysUser.getRole().getRights()) {
                info.addStringPermission(right.getRightCode());
            }
        }
        System.out.println("进入CustoRealm:doGetAuthorizationInfo方法");

        return info;
    }


    /**
     * 定义认证域
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken token= (UsernamePasswordToken) authenticationToken;
        String userNmae= token.getUsername();

//        String password=new String(token.getPassword()) ;

        SysUserVo sysUser= sysUserService.getUserByUsrName(userNmae);

        if(sysUser==null){
            throw new UnknownAccountException();
        }

        if(sysUser.getUsrFlag()!=1){
            throw new LockedAccountException();
        }
        MySimpleByteSource salt = new MySimpleByteSource("czkt");

        SimpleAuthenticationInfo info =new SimpleAuthenticationInfo(sysUser,sysUser.getUsrPassword(), salt,String.valueOf(sysUser.getUsrId()));

        return info;
    }


    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }



}
